﻿

using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace Bzure.Common.AOP
{

    /// <summary>
    /// Dynamic aspect extension that initializes MEF container, as well as extension method to create dynamic object for weavable object.
    /// </summary>
    public static class WeaverExtension
    {
        /// <summary>
        /// A MEF container that is used by DynamicAspect, do not suggest for use.
        /// </summary>
         public static CompositionContainer Container = null;

         static WeaverExtension()
        {
             AggregateCatalog catalog =new AggregateCatalog( 
                                            new DirectoryCatalog(@".\"),
                                            new DirectoryCatalog(@".\", "*.exe"));

             Container = new CompositionContainer(catalog);
        }

        /// <summary>
        /// Extension mathod to create <see cref="WeavableObject<T>"/>  class that make target object can be weaved runtime.
        /// </summary>
        /// <typeparam name="T">Type of target object</typeparam>
        /// <param name="source">target object instance</param>
        /// <returns>An instance of  <see cref="WeavableObject<T> "/></returns>
         public static WeavableObject<T> AsDynamic<T>(this T source) where T : class
         {
             var result = new WeavableObject<T>(source);
             Container.ComposeParts(result);
             return result;
         }
    }
}